package fun.sssdnsy.controller;

import fun.sssdnsy.util.ConfigUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import java.util.Map;

/**
 * 快捷键配置控制器
 */
public class ShortcutConfigController {
    @FXML private Label lblSave;
    @FXML private TextField txtSave;
    @FXML private Label lblOpen;
    @FXML private TextField txtOpen;
    @FXML private Label lblCopy;
    @FXML private TextField txtCopy;
    @FXML private Label lblPaste;
    @FXML private TextField txtPaste;
    @FXML private Label lblBold;
    @FXML private TextField txtBold;
    @FXML private Label lblItalic;
    @FXML private TextField txtItalic;
    @FXML private Button btnSave;

    private Map<String, String> shortcuts;

    @FXML
    private void initialize() {
        shortcuts = ConfigUtil.getShortcuts();
        loadShortcuts();
        setupListeners();
    }

    /**
     * 加载快捷键配置到界面
     */
    private void loadShortcuts() {
        txtSave.setText(shortcuts.getOrDefault("save", "Ctrl+S"));
        txtOpen.setText(shortcuts.getOrDefault("open", "Ctrl+O"));
        txtCopy.setText(shortcuts.getOrDefault("copy", "Ctrl+C"));
        txtPaste.setText(shortcuts.getOrDefault("paste", "Ctrl+V"));
        txtBold.setText(shortcuts.getOrDefault("bold", "Ctrl+B"));
        txtItalic.setText(shortcuts.getOrDefault("italic", "Ctrl+I"));
    }

    /**
     * 监听快捷键输入
     */
    private void setupListeners() {
        txtSave.textProperty().addListener((obs, oldVal, newVal) -> shortcuts.put("save", newVal));
        txtOpen.textProperty().addListener((obs, oldVal, newVal) -> shortcuts.put("open", newVal));
        txtCopy.textProperty().addListener((obs, oldVal, newVal) -> shortcuts.put("copy", newVal));
        txtPaste.textProperty().addListener((obs, oldVal, newVal) -> shortcuts.put("paste", newVal));
        txtBold.textProperty().addListener((obs, oldVal, newVal) -> shortcuts.put("bold", newVal));
        txtItalic.textProperty().addListener((obs, oldVal, newVal) -> shortcuts.put("italic", newVal));
    }

    /**
     * 保存快捷键配置
     */
    @FXML
    private void onSaveShortcuts() {
        ConfigUtil.setShortcuts(shortcuts);
        ConfigUtil.save();
    }

    public void onRestoreDefaults(ActionEvent actionEvent) {
    }

    public void onApply(ActionEvent actionEvent) {

    }

    public void onCancel(ActionEvent actionEvent) {

    }
}
